package com.gamesnet.ihm;

import java.util.List;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

import com.gamesnet.api.Api;
import com.gamesnet.api.Session;
import com.gamesnet.models.Listable;
import com.gamesnet.models.Message;
import com.gamesnet.models.Utilisateur;
import com.gamesnet.util.AdapterListener;
import com.gamesnet.util.MessageDialog;
import com.gamesnet.widget.ListAdapterMessageEnvoye;

public class MessageEnvoyeActivity extends Activity implements AdapterListener{

	@Override
	public void onCreate(Bundle savedInstanceState) { 

		super.onCreate(savedInstanceState); 
		setContentView(R.layout.messagesrecu);
		Utilisateur currentUser = Session.getInstance().getUser();

		try {
			List<Message>messages = Api.messagesEnvoyes(String.valueOf(currentUser.getId()), "0", "10");
			ListAdapterMessageEnvoye<Message> adapter = new ListAdapterMessageEnvoye<Message>(this, messages);
			adapter.addListener(this);
			ListView list = (ListView)findViewById(R.id.listMessagesRecu);
			list.setAdapter(adapter);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void onClickText(Listable model, int position) {
		Message m = (Message)model;
		startActivity(m, position);
	}

	public void onClickSupp(Listable model, int position) {
		MessageDialog.showMessage("Supprimer : "+String.valueOf(model.getId()), this);
	}
	
	public void startActivity(Message message, int postion){
		Intent intent = new Intent(this, DetailMessageEnvoyeActivity.class);
		intent.putExtra("messageId", message.getId());
		intent.putExtra("destinataireId", message.getDestinataires().getId());
		startActivity(intent);
	}

}
